package com.cyms.modules.base.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.cyms.modules.sys.controller.AbstractController;
import org.apache.commons.lang.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.cyms.modules.base.entity.LineTypeEntity;
import com.cyms.modules.base.service.LineTypeService;
import com.cyms.modules.sys.entity.SysMenuEntity;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.cyms.common.utils.PageUtils;
import com.cyms.common.utils.R;



/**
 * 线路分类
 *
 * @author low
 * @email tolower@foxmail.com
 * @date 2018-12-05 22:19:22
 */
@RestController
@RequestMapping("base/linetype")
public class LineTypeController extends AbstractController {
    @Autowired
    private LineTypeService lineTypeService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("base:linetype:list")
    public R list(@RequestParam Map<String, Object> params){
    	List<LineTypeEntity> list=lineTypeService.selectList(
    	        new EntityWrapper<LineTypeEntity>()
                        .orderBy("sort_num", true)
                        .eq("create_org",getUser().getCompanyId()));
    	
        return R.ok().put("list", list);
    }
    /**
	 * 选择菜单(添加、修改菜单)
	 */
	@GetMapping("/select")
	@RequiresPermissions("base:linetype:info")
	public R select(){
		//查询列表数据
		List<LineTypeEntity> typeList = lineTypeService.selectList(
		        new EntityWrapper<LineTypeEntity>()
                        .eq("create_org",getUser().getCompanyId())
                        .orderBy("sort_num", true));
		
		//添加顶级菜单
		LineTypeEntity root = new LineTypeEntity();
		root.setId(0L);
		root.setName("一级分类");
		root.setPid(-1L);
		root.setOpen(true);
		typeList.add(root);
		
		return R.ok().put("typeList", typeList);
	}

    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("base:linetype:info")
    public R info(@PathVariable("id") Long id){
			LineTypeEntity lineType = lineTypeService.selectById(id);

        return R.ok().put("lineType", lineType);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("base:linetype:save")
    public R save(@RequestBody LineTypeEntity lineType){
        lineType.setCreateDept(getUser().getDeptId());
        lineType.setCreateOrg(getUser().getCompanyId());
        lineType.setCreateUser(getUserId());
    	lineTypeService.insert(lineType);
    	//
    	LineTypeEntity pLineType=lineTypeService.selectById(lineType.getPid());
    	if (lineType.getPid() != 0 && StringUtils.isNotBlank(pLineType.getPids())) {
    		lineType.setPids(pLineType.getPids()+lineType.getId()+",");
		} else {
			lineType.setPids("0,"+lineType.getId()+",");
		}
    	lineTypeService.insertOrUpdate(lineType);
        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("base:linetype:update")
    public R update(@RequestBody LineTypeEntity lineType){
    	LineTypeEntity pLineType=lineTypeService.selectById(lineType.getPid());
    	if (lineType.getPid() != 0 && StringUtils.isNotBlank(pLineType.getPids())) {
    		lineType.setPids(pLineType.getPids()+lineType.getId()+",");
		} else {
			lineType.setPids("0,"+lineType.getId()+",");
		}
		lineTypeService.updateById(lineType);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("base:linetype:delete")
    public R delete(@RequestBody Long[] ids){
			lineTypeService.deleteBatchIds(Arrays.asList(ids));

        return R.ok();
    }

}
